package com.ruoyi.web.controller.comlbs;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.IdName;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.YesNoEnum;
import com.ruoyi.system.domain.MessReceive;
import com.ruoyi.system.domain.MessSend;
import com.ruoyi.system.service.IMessReceiveService;
import com.ruoyi.system.service.IMessSendService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 发送消息Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/messSend")
public class MessSendController extends BaseController {

    @Autowired
    private IMessSendService messSendService;
    @Autowired
    private IMessReceiveService messReceiveService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询发送消息列表
     */
    @ApiOperation(("发送消息列表"))
    @ApiOperationSupport(includeParameters = {"startTime","endTime"})
    @GetMapping("/list")
    public TableDataInfo list(MessSend messSend) {
        startPage();
        List<MessSend> list = messSendService.selectList(messSend);
        return getDataTable(list);
    }

    /**
     * 查询发送消息列表
     */
    @ApiOperation(("接收消息列表"))
    @ApiOperationSupport(includeParameters = "sendId")
    @GetMapping("/receive/list")
    public AjaxResult receiveList(MessReceive messReceive) {
        return success(messReceiveService.selectList(messReceive));
    }

    /**
     * 获取发送消息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(messSendService.getDetail(new MessSend().setId(id)));
    }

    /**
     * 新增发送消息
     */
    @ApiOperation("下发消息")
    @ApiOperationSupport(includeParameters = {"title","messType","content","sendTime","receiveUserId"})
    @Log(title = "发送消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MessSend messSend) {
        messSendService.addMessSend(messSend);
        return success();
    }

    /**
     * 修改发送消息
     */
    @Log(title = "发送消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MessSend messSend) {
        return toAjax(messSendService.updateById(messSend));
    }

    @ApiOperation("删除发送消息")
    @Log(title = "发送消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        messSendService.update(new MessSend().setIsDel(YesNoEnum.YES.getCode()), new QueryWrapper<MessSend>().in("id", ids));
        return success();
    }

    @ApiOperation("角色用户列表")
    @ApiOperationSupport(includeParameters = "roleId")
    @GetMapping("/roleUser/list")
    public AjaxResult allocatedListAll(SysUser user) {
        List<IdName> resultList = userService.selectAllocatedList(user).stream().map(i ->
                new IdName(i.getUserId().toString(), i.getNickName())).collect(Collectors.toList());
        return success(resultList);
    }
}
